package com.jwthhealth_pub.old.sign.view;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.sign.model.FindPwModle;
import com.jwthhealth_pub.old.sign.presenter.ISignPwPresenter;
import com.jwthhealth_pub.old.sign.presenter.ISignPwPresenterCompl;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 修改密码
 * Created by liuzhen000 on 2016/11/17.
 */
public class SignPwActivity extends Activity implements View.OnClickListener {

    public static final int HIDELAYOUTOPT = 0;
    public static final int SHOWLAYOUTOPT = 1;
    public static final int CHANGEDSUCCESS = 2;
    public static final int SMSCODE = 3;

    public static final String PWMOUDLE = "foundpasswd";

    @BindView(R.id.btn_get_verification_code)
    Button btnGetVerificationCode;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.title_layout)
    TitleLayout titleLayout;

    private View commitBtn, commitBtnSecStep;

    private View mFindPwLayout, mChangePwLayout;
    private TextView mBackBtn;
    private EditText mUserNameEd, mPhoneNumEd, mCodeEd, mNewPwEd, mCorrectPwEd;
    private ISignPwPresenter iSignPwPresenter;

    private FindPwModle mFindPwModle;

    public Boolean isRequesting;

    public Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case HIDELAYOUTOPT:
                    mFindPwModle = (FindPwModle) msg.getData().getSerializable(PWMOUDLE);
                    showOptionLayout(false);
                    break;
                case SHOWLAYOUTOPT:
                    showOptionLayout(true);
                    break;
                case CHANGEDSUCCESS:
                    Toast.makeText(SignPwActivity.this, R.string.sign_find_pw_change_hint_success, Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case SMSCODE:
                    String message = msg.getData().getString(Constant.SIGN_TOKEN);
                    Toast.makeText(SignPwActivity.this, message, Toast.LENGTH_SHORT).show();
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_pw);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        ButterKnife.bind(this);

        isRequesting = false;

        commitBtnSecStep = findViewById(R.id.btn_sign_pw_commit);
        commitBtnSecStep.setOnClickListener(this);
        this.commitBtn = findViewById(R.id.btn_sign_pw_confirm);
        this.commitBtn.setOnClickListener(this);
        findViewById(R.id.btn_sign_pw_commit).setOnClickListener(this);
        btnGetVerificationCode.setOnClickListener(this);
        mPhoneNumEd = (EditText) findViewById(R.id.ed_sign_pw_phone);
        mCodeEd = (EditText) findViewById(R.id.ed_sign_pw_verification_code);
        mNewPwEd = (EditText) findViewById(R.id.ed_sign_up_pw);
        mCorrectPwEd = (EditText) findViewById(R.id.ed_sign_up_confirm_pw);

//        TextChange textChange = new TextChange();
//        mPhoneNumEd.addTextChangedListener(textChange);
//        mCodeEd.addTextChangedListener(textChange);
//        mNewPwEd.addTextChangedListener(textChange);
//        mCorrectPwEd.addTextChangedListener(textChange);

        mFindPwLayout = findViewById(R.id.layout_pw_find);
        mChangePwLayout = findViewById(R.id.layout_pw_change);

        iSignPwPresenter = new ISignPwPresenterCompl(this);

        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                if (mChangePwLayout.getVisibility() == View.VISIBLE) {
                    mChangePwLayout.setVisibility(View.GONE);
                    mFindPwLayout.setVisibility(View.VISIBLE);
                } else {
                    finish();
                }
            }

            @Override
            public void leftTextClick() {

            }


            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    public void progress() {
        if (progressbar.getVisibility() == View.GONE) {
            progressbar.setVisibility(View.VISIBLE);
        } else {
            progressbar.setVisibility(View.GONE);
        }
    }

    class TextChange implements TextWatcher {

        @Override
        public void afterTextChanged(Editable arg0) {

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {

        }

        @Override
        public void onTextChanged(CharSequence cs, int start, int before,
                                  int count) {
            if (mPhoneNumEd.getText().toString().trim().length() > 0) {
                btnGetVerificationCode.setEnabled(true);
            } else {
                btnGetVerificationCode.setEnabled(false);
            }

            if (mCodeEd.getText().toString().trim().length() > 0) {
                commitBtn.setEnabled(true);
            } else {
                commitBtn.setEnabled(false);
            }

            if (mCorrectPwEd.getText().toString().trim().length() > 0 && mNewPwEd.getText().toString().trim().length() > 0) {
                commitBtnSecStep.setEnabled(true);
            } else {
                commitBtnSecStep.setEnabled(false);
            }
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_sign_pw_confirm:
                String phoneNum = mPhoneNumEd.getText().toString().trim();
                String code = mCodeEd.getText().toString().trim();
                if (phoneNum.length() <= 0) {
                    Toast.makeText(this, R.string.sign_find_pw_phone_hint, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (code.length() <= 0) {
                    Toast.makeText(this, R.string.sign_find_pw_testing_hint, Toast.LENGTH_SHORT).show();
                    return;
                }

                if (isRequesting) {
                    return;
                }

                isRequesting = true;
                progress();
                iSignPwPresenter.findPw(phoneNum, code);
                break;
            case R.id.btn_sign_pw_commit:
                String pw = mNewPwEd.getText().toString().trim();
                String correctPw = mCorrectPwEd.getText().toString().trim();

                if (pw.length() <= 0) {
                    Toast.makeText(this, R.string.sign_find_pw_change_hint, Toast.LENGTH_SHORT).show();
                    break;
                } else if (correctPw.length() <= 0) {
                    Toast.makeText(this, R.string.sign_find_pw_change_hint_again, Toast.LENGTH_SHORT).show();
                    break;
                }


                if (!pw.equals(correctPw)) {
                    Toast.makeText(this, R.string.sign_find_pw_change_inconformity, Toast.LENGTH_SHORT).show();
                    break;
                }

                if (isRequesting) {
                    break;
                }

                if (mFindPwModle != null) {
                    FindPwModle.DataBean data = mFindPwModle.getData();
                    isRequesting = true;
                    iSignPwPresenter.setNewPw(data.getPhone(), data.getToken(), pw);
                }

                break;
            case R.id.btn_get_verification_code:
                String sth = mPhoneNumEd.getText().toString().trim();
                iSignPwPresenter.smsCode(sth, PWMOUDLE);
                break;
            default:
                break;
        }
    }

    public void showOptionLayout(boolean isFindPw) {
        if (isFindPw) {
            mFindPwLayout.setVisibility(View.VISIBLE);
            mChangePwLayout.setVisibility(View.GONE);
        } else {
            mFindPwLayout.setVisibility(View.GONE);
            mChangePwLayout.setVisibility(View.VISIBLE);
        }
    }
}
